//压缩上传图片或上传普通文件。
document.getElementById("file2").onchange = function() {
	var currFile = this.files[0];
    if (currFile.name.indexOf("&") != -1 || currFile.name.indexOf(";") != -1) {
        alert("文件名含有特殊字符，请检查后再试！");
		//layer.msg("文件名含有特殊字符，请检查！");
		return;
    }
    if (currFile.name.toLowerCase().indexOf(".webp") > 0) {
        alert("不允许上传webp类型的文件！");
        return;
    }

	var reader = new FileReader();
	reader.onloadend = function(e) {
        var src = e.target.result; //src形如"..."
		var ext0 = currFile.name.toLowerCase().split('.').splice(-1);
        if (ext0 == "jpg" || ext0 == "jpeg" || ext0 == "gif" || ext0 == "png" || ext0 == "bmp" || ext0 == "ico") {
			dealImage2(src, 1000, currFile.name, printing2);
		} else {
			printing2(src, currFile.name);
		}
	};
	reader.readAsDataURL(currFile);
};
//使用压缩
function dealImage2(base64, w, fname, callback) {
	var newImage = new Image();
	var quality = 0.7; //压缩系数0-1之间
	newImage.src = base64;
	newImage.setAttribute("crossOrigin", 'Anonymous'); //url为外域时需要
	var imgWidth, imgHeight;
	newImage.onload = function() {
		imgWidth = this.width;
		imgHeight = this.height;
		var canvas = document.createElement("canvas");
		var ctx = canvas.getContext("2d");
		if (Math.max(imgWidth, imgHeight) > w) {
			if (imgWidth > imgHeight) {
				canvas.width = w;
				canvas.height = w * imgHeight / imgWidth;
			} else {
				canvas.height = w;
				canvas.width = w * imgWidth / imgHeight;
			}
		} else {
			canvas.width = imgWidth;
			canvas.height = imgHeight;
			quality = 0.7;
		}
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
		var base64 = canvas.toDataURL("image/jpeg", quality); //压缩语句
		// 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间，请加以下语句，quality初始值根据情况自定
		// while (base64.length / 1024 > 150) {
		// 	quality -= 0.01;
		// 	base64 = canvas.toDataURL("image/jpeg", quality);
		// }
		// 防止最后一次压缩低于最低尺寸，只要quality递减合理，无需考虑
		// while (base64.length / 1024 < 50) {
		// 	quality += 0.001;
		// 	base64 = canvas.toDataURL("image/jpeg", quality);
		// }
		callback(base64, fname); //必须通过回调函数返回，否则无法及时拿到该值
	};
}
//压缩回调
function printing2(base64, fname) {
	$.post("/Home/UpBase64/", {
		Base64Image: base64,
		fileName: fname
	}, function(data) {
		if (data.success) {
			var ext = data.message.toLowerCase().split('.').splice(-1);
            if (ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "png" || ext == "bmp" || ext == "ico" ) {
                $(".ul0").append("<li><div style = 'width:60px;height:60px;overflow:hidden' ><img src='/UploadFiles/" + data.message + "' class='upimg' style='width:60px;height:60px;' /><div class='cornerClose' data-ng-src='" + data.message + "' onclick='remove(this)' >&nbsp;&nbsp;&times;</div></div></li >"); //.attr("src", "/UpFile/" + data);
            } else {
                $(".ul1").append("<li class='list-group-item'><a href='/UploadFiles/"+data.message+"' download='data.message.substr(37)'><span class='glyphicon glyphicon-paperclip'></span><span class='ul1Text'>" + data.message.substr(37) + "</span></a><span><button type='button' class='close' aria-label='Close' data-ng-src='" + data.message + "' onclick='remove(this)'><span aria-hidden='true'>&times;</span></button></span></li>");
			}
			$("#AttachmentPaths").val($("#AttachmentPaths").val() + data.message + ";");
		}
	}, "json");
}

function remove(obj) {
	var imgsr = $(obj).attr("data-ng-src");
	$("#AttachmentPaths").val($("#AttachmentPaths").val().replace(imgsr + ";", ""));
	$(obj).parent().parent().remove();
}
